|
|
|
|
|
|
|
Here the symbol S means to add the values for all of the data points (xi, yi). |
|
|
|
|
|
|
|
|
If you think your data points lie more or less along a straight line, you can write the equation of a line to fit the points as y = m * x + b. Then the sum of the squares of the distances from the line to the data points becomes: |
|
|
|
 |
|
|
|
|
E(m, b) = S (m * xi + b - yi)2 |
|
|
|
|
|
|
|
|
You can find the minimum of this equation using a little calculus. If your calculus is rusty, you can either take this on faith or skip down to the results. |
|
|
|
|
|
|
|
|
Take the partial derivatives of E(m, b) with respect to m and b, set the derivatives equal to zero, and solve for the values of m and b. Those values give the equation for the line that minimizes the error equation E(F(x)). The partial derivatives are given by: |
|
|
|
 |
|
|
|
|
dE/dm = S 2 * (m * xi + b - yi) * xi = m * S 2 * xi2 + b * S 2 * xi - S 2 * xi * yi dE/db = S 2 * (m * xi + b - yi)
= m * S 2 * xi + b * S 2 - S 2 * yi |
|
|
|
|
|
|
|
|
By setting these equations equal to zero and rearranging a bit, you get: |
|
|
|
 |
|
|
|
|
m * S xi2 + b * S xi = S xi * yi m * S xi + b * S 1 = S yi |
|
|
|
|
|
|
|
|
Keep in mind that the xi and yi are just numbers that define the positions of your data points. That means the values of S xi2, S xi, S xi * yi, and S yi are only numbers that you can easily compute. For convenience, calculate these values and let: |
|
|
|
 |
|
|
|
|
A = S xi2 B = S xi C = S xi * yi D = S yi |
|
|
|
|
|
|
|
|
If you have N data points numbered from 1 to N, the value of S1 is simply N. Substituting all of these values into the previous equations, you get: |
|
|
|
 |
|
|
|
|
m * A + b + B = C
m * B + b * N = D |
|
|
|
|
|
|
|
|
This gives you two equations with two unknowns. Solving them for m and b gives: |
|
|
|
 |
|
|
|
|
m = (B * D - C * N) / (B2 - A * N)
b = (B * C - A * D) / (B2 - A * N) |
|
|
|
|
|
|
|
|
Notice that these values are undefined if B2 A * N = 0. That happens when all of the data points have the same X coordinate x1. In that case: |
|
|
|
|
|